package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.util;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;

import org.icefaces.application.PushRenderer;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.DeviceService;

public class PresentationUtil {
	
	public static String DEVICE_GROUP = "@Device.";
	
	public static Object getManagedBean(String beanName) {
        FacesContext fc = FacesContext.getCurrentInstance();
        ELContext elc = fc.getELContext();
        ExpressionFactory ef = fc.getApplication().getExpressionFactory();
        ValueExpression ve = ef.createValueExpression(elc, getJsfEl(beanName), Object.class);
        return ve.getValue(elc);
    }
	
	private static String getJsfEl(String value) {
        return "#{" + value + "}";
    }
	public static void addDeviceToCurrentView(DeviceService dev){
		PushRenderer.addCurrentView(dev.getDeviceID()+ DEVICE_GROUP);
	}
	public static void removeDeviceToCurrentView(DeviceService dev){
		PushRenderer.removeCurrentView(dev.getDeviceID()+ DEVICE_GROUP);
	}
}
